/***************************************************************************
 创建者: 华磊
 开始时间: 2018.8.14
 copyright: (C) 华友高科
 修改说明: (每次有修改就添加一条，带有 修改人，修改时间，修改描述)
 example (1) hualei 2017.5.8 类的具体实现代码编写

 ***************************************************************************/
#include "sysinfoget.h"

SysInfoGet::SysInfoGet()
{
    path = NULL;
    pathUsb = NULL;
    ent = NULL;

//    getSysInfoInit();           //初始化
//    getSysInfo();               //获取
}

void SysInfoGet::getSysInfoInit()
{
    //初始化usb加载地址
    struct passwd *pwd = new passwd;
    pwd = getpwuid(getuid());

    path = new char(15);
    strcpy(path, "/media/");
    strcat(path, pwd->pw_name); // ./media/root

//    if(NULL != pwd)
//    {
//        delete pwd;
//        pwd = NULL;
//    }

    //初始化
    pathUsb = new char(25);     //长度够不够?

    ent = new dirent;

    //数据
    MBsize = 1024.0*1024.0;     //最终结果只留下两位
    totalDiskSize=0;
    usedDiskSize=0;
    totalMemSize=0;
    usedMemSize=0;
    totalUsbSize=0;
    usedUsbSize=0;
    usbExist=false;
}

void SysInfoGet::getSysInfo()
{
    //--------磁盘信息测试---------------------------
    //struct statfs diskInfo;
    statfs("/", &diskInfo);

    totalDiskSize = (double)(diskInfo.f_bsize*diskInfo.f_blocks)/MBsize;
    usedDiskSize = (double)(diskInfo.f_bsize*diskInfo.f_blocks-diskInfo.f_bsize*diskInfo.f_bfree)/MBsize;

    //qDebug()<<"total:"<<totalDiskSize<<"MB";
    //qDebug()<<"used :"<<usedDiskSize<<"MB";

    //---------内存信息测试----------------------------
    //struct sysinfo memInfo;
    sysinfo(&memInfo);

    totalMemSize = (double)memInfo.totalram/MBsize;
    usedMemSize = (double)(memInfo.totalram-memInfo.freeram)/MBsize;

    //qDebug()<<"totalRAM:"<<totalMemSize<<"MB";
    //qDebug()<<"used RAM:"<<usedMemSize<<"MB";

    //---------U盘信息测试------------------------------
    DIR *pDir = opendir(path);

//    int existFlag;
//    existFlag = 0;
//    while(NULL != (ent=readdir(pDir)) )
//    {
//        if(ent->d_type==4)//if(ent->d_type & DT_DIR)
//        {
//            if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0) continue;

//            //qDebug()<<ent->d_name;//u盘文件夹名称
//            strcpy(pathUsb, path);
//            strcat(pathUsb, "/");
//            strcat(pathUsb, ent->d_name);
//            //qDebug()<<pathUsb;//u盘文件夹地址

//            struct statfs diskInfo2;
//            statfs(pathUsb, &diskInfo2);
//            totalUsbSize = (double)(diskInfo2.f_bsize*diskInfo2.f_blocks)/MBsize;
//            usedUsbSize = (double)(diskInfo2.f_bsize*diskInfo2.f_blocks-diskInfo2.f_bsize*diskInfo2.f_bfree)/MBsize;
//            existFlag=1;

//            //qDebug()<<"totalUSB :"<<totalUsbSize<<"MB";
//            //qDebug()<<"used USB :"<<usedUsbSize<<"MB";
//        }
//    }
//    closedir(pDir);//必须关闭
//    if(existFlag == 1){
//        usbExist = true;
//    }
//    else{
//        usbExist = false;
//    }

    //此获取的是最新插入的那个usb(一个)
    ent=readdir(pDir);                  //UUI1;UUI;..;.
    //qDebug()<<"test "<<ent->d_name;
    if(ent->d_type==4)
    {                 //属于文件夹
        if(strcmp(ent->d_name,".")==0 || strcmp(ent->d_name,"..")==0)
        {
            usbExist = false;
            totalUsbSize = 0;
            usedUsbSize = 0;
        }
        else
        {
            strcpy(pathUsb, path);
            strcat(pathUsb, "/");
            strcat(pathUsb, ent->d_name);   //获取u盘地址

            //struct statfs diskInfo2;
            statfs(pathUsb, &diskInfo2);

            usbExist = true;
            totalUsbSize = (double)(diskInfo2.f_bsize*diskInfo2.f_blocks)/MBsize;
            usedUsbSize = (double)(diskInfo2.f_bsize*diskInfo2.f_blocks-diskInfo2.f_bsize*diskInfo2.f_bfree)/MBsize;
            //qDebug()<<"totalUSB :"<<totalUsbSize<<"MB";
            //qDebug()<<"used USB :"<<usedUsbSize<<"MB";
        }
    }
    closedir(pDir);                     //必须关闭

//    if(NULL != path)
//    {
//        delete path;
//        path = NULL;
//    }
//    if(NULL != pathUsb)
//    {
//        delete pathUsb;
//        pathUsb = NULL;
//    }
//    if(NULL != ent)
//    {
//        delete ent;
//        ent = NULL;
//    }
//    if(NULL != pDir)
//    {
//        delete pDir;
//        pDir = NULL;
//    }
    //qDebug()<<"USB :"<<usbExist;
}

